﻿using BarcodeDelay.bean;
using BarcodeDelay.business;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using RestSharp.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BarcodeDelay.HttpClient
{
    public class HttpUtility
    {
        public static bool getMyInfo(string postData)
        {
            bool res = false;
            string strRes = "";
            try
            {
                string strAdr = @"/v2/try_info";
                strAdr = StoParams.Instance.contact + strAdr;
                var client = new RestClient(strAdr);
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddHeader("Content-Type", "application/json");
                request.AddParameter("application/json", postData, ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
                JObject jsonObj = (JObject)JsonConvert.DeserializeObject(response.Content);
                JToken dayQueryCount = (JToken)jsonObj["queryLimit"];
                JToken QueryUrl = (JToken)jsonObj["queryUrl"];
                JToken status = (JToken)jsonObj["status"];
                JToken todayLeft = (JToken)jsonObj["todayLeft"];
                JToken endDate = (JToken)jsonObj["endDate"];
                int istatus = Convert.ToInt32(status.ToString());

                int idayQueryCount = Convert.ToInt32(dayQueryCount.ToString());
                StoParams.Instance.day_total = idayQueryCount;
                StoParams.Instance.queryUrl = QueryUrl.ToString();
                StoParams.Instance.user_status = istatus;
                StoParams.Instance.endDate = endDate.ToString();


                StoParams.Instance.day_left = Convert.ToInt32(todayLeft.ToString());
                if (istatus != 1)
                {
                    StoParams.Instance.day_left = 0;
                }
                res = true;

            }
            catch(Exception ex)
            {

            }
            return res;
        }



        public static bool getContactInfo(string postData)
        {
            bool res = false;
            string strRes = "";
            try
            {
                string strAdr = @"/v2/member_info";
                strAdr = StoParams.Instance.contact + strAdr;
                var client = new RestClient(strAdr);
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddHeader("Content-Type", "application/json;charset=UTF-8");
                request.AddParameter("application/json", postData, ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
                if(response.ErrorMessage!=null&&response.ErrorMessage.Contains("无法连接到远程服务器"))
                {
                    //System.Windows.Forms.MessageBox.Show("无法连接到远程服务器,请联系客服");
                    return res;
                }
                JObject jsonObj = (JObject)JsonConvert.DeserializeObject(response.Content);
                JToken msg = (JToken)jsonObj["msg"];
                JObject jsonObjSite= (JObject)jsonObj["siteTimeMap"];

                JObject jsonObjFarSite = (JObject)jsonObj["farCompanyMap"];

                JObject jsonObjNextDaySite = (JObject)jsonObj["nextDayCompanyMap"];
                if (jsonObjSite!=null)
                {
                    IEnumerable<JProperty> properties = jsonObjSite.Properties();
                    if (properties != null)
                    {
                        foreach (JProperty item in properties)
                        {
                            string key = item.Name;
                            object type = item.Value;
                            MemberInfo.ht_Each_Center_Time[key] = type.ToString();

                        }
                    }
                }

                if (jsonObjFarSite != null)
                {
                    IEnumerable<JProperty> properties = jsonObjFarSite.Properties();
                    if (properties != null)
                    {
                        foreach (JProperty item in properties)
                        {
                            string key = item.Name;
                            object type = item.Value;
                            FarCompany farCompany = new FarCompany();
                            string[] arrKey = key.Split('-');
                            farCompany.priorStie = arrKey[0].ToString();
                            farCompany.companyName= arrKey[1].ToString();
                            farCompany.addDays = Convert.ToInt32(type.ToString());
                            MemberInfo.ht_Far_Company[key] = farCompany;

                        }
                    }
                }

                if (jsonObjNextDaySite != null)
                {
                    IEnumerable<JProperty> properties = jsonObjNextDaySite.Properties();
                    if (properties != null)
                    {
                        foreach (JProperty item in properties)
                        {
                            string key = item.Name;
                            object type = item.Value;
                            NextDayCompany farCompany = new NextDayCompany();    
                            MemberInfo.ht_NextDay_Company[key] = type;
                        }
                    }
                }


                if (msg.ToString().Contains("unreg")) 
                {
                    StoParams.Instance.day_total = 0;
                    StoParams.Instance.day_left = 0;
                    StoParams.Instance.user_status = 2;
                    res = true;
                    return res;
                }
                if (msg.ToString().Contains("expire"))
                {
                    StoParams.Instance.day_total = 0;
                    StoParams.Instance.day_left = 0;
                    StoParams.Instance.user_status = 3;
                    res = true;
                    return res;
                }
                JToken dayQueryCount = (JToken)jsonObj["queryLimit"];
                JToken QueryUrl = (JToken)jsonObj["queryUrl"];
                JToken dayLeftCount = (JToken)jsonObj["todayLeft"];
                JToken regCode = (JToken)jsonObj["regCode"];
                JToken endDate = (JToken)jsonObj["endDate"];
                int idayQueryCount = Convert.ToInt32(dayQueryCount.ToString());
                StoParams.Instance.day_total = idayQueryCount;
                StoParams.Instance.queryUrl = QueryUrl.ToString();
                StoParams.Instance.day_left = Convert.ToInt32(dayLeftCount);
                StoParams.Instance.regCode = regCode.ToString();
                StoParams.Instance.endDate = endDate.ToString();
                res = true;

            }
            catch (Exception ex)
            {

            }
            return res;
        }


        public static string getServerTime(string postData)
        {
            bool res = false;
            string strRes = "";
            try
            {
                string strAdr = @"/v2/time_info";
                strAdr = StoParams.Instance.contact + strAdr;
                var client = new RestClient(strAdr);
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddHeader("Content-Type", "application/json");
                request.AddParameter("application/json", postData, ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
                JObject jsonObj = (JObject)JsonConvert.DeserializeObject(response.Content);
                JToken nowTime = (JToken)jsonObj["nowTime"];
                strRes = nowTime.ToString();
                res = true;

            }
            catch (Exception ex)
            {

            }
            return strRes;
        }


        public static JObject regInfo(string postData)
        {
            bool res = false;
            string strRes = "";
            JObject jsonObj = null;
            try
            {
                string strAdr = @"/v2/member_reg";
                strAdr = StoParams.Instance.contact + strAdr;
                var client = new RestClient(strAdr);
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddHeader("Content-Type", "application/json");
                request.AddParameter("application/json", postData, ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
                jsonObj = (JObject)JsonConvert.DeserializeObject(response.Content);
                JToken msg = (JToken)jsonObj["msg"];               
                res = true;

            }
            catch (Exception ex)
            {

            }
            return jsonObj;
        }


        public static JObject updateDayCount(string postData)
        {
            bool res = false;
            string strRes = "";
            JObject jsonObj = null;
            try
            {
                string adr = @"/v2/count_time";
                adr = StoParams.Instance.contact + adr;
                var client = new RestClient(adr);
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddHeader("Content-Type", "application/json");
                request.AddParameter("application/json", postData, ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
                jsonObj = (JObject)JsonConvert.DeserializeObject(response.Content);
                res = true;

            }
            catch (Exception ex)
            {

            }
            return jsonObj;
        }


    }
}
